/**
 * 文件描述：用户登录 Servlet
 * 创建人： SPGLP55
 * 创建时间：2024/10/31
 * 最后编辑人：SPGLP55
 * 最后编辑时间：2024/11/1
 * 文件版本：3
 */

package com.gxuwz.lhleavesys.web.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gxuwz.lhleavesys.context.AppContext;
import com.gxuwz.lhleavesys.service.impl.StudentServiceImpl;
import com.gxuwz.lhleavesys.service.impl.TeacherServiceImpl;
import com.gxuwz.lhleavesys.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.IOException;
import java.sql.SQLException;

import com.gxuwz.lhleavesys.entity.*;
import com.gxuwz.lhleavesys.service.*;

@WebServlet(name = "LoginLHServlet", urlPatterns = "/login")
public class LoginLHServlet extends BaseLHServlet {

    @Autowired
    UserService userService;

    @Autowired
    TeacherService teacherService;

    @Autowired
    StudentService studentService;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.teacherService =(TeacherServiceImpl) AppContext.getBean("teacherServiceImpl", TeacherServiceImpl.class);
            this.userService =(UserServiceImpl) AppContext.getBean("userServiceImpl",UserServiceImpl.class);
            this.studentService =(StudentServiceImpl) AppContext.getBean("studentServiceImpl",StudentServiceImpl.class);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uName = req.getParameter("username");
        String uPwd = req.getParameter("userPassword");
        String uRole = req.getParameter("userRole");

        if (uName == null || uPwd == null || uRole == null) {
            doDispatch(req, resp, "/login.jsp");
            return;
        }

        // 用户验证
        try {
            System.out.println("uName: " + uName + ", uPwd: " + uPwd + ", uRole: " + uRole);
//            System.out.println(userService.toString());
            if (userService.loginCheck(uName, uPwd, uRole)) {
                HttpSession session = req.getSession();
                session.setAttribute("username", uName);
                session.setAttribute("role", uRole);
                /// 跳转到用户主页
                if (uRole.equals("teacher")) {
                    doDispatch(req,resp, "/views/teacher/home.jsp");
                } else if (uRole.equals("student")) {
                    doDispatch(req,resp, "/views/student/home.jsp");
                } else if (uRole.equals("system")) {
                    doDispatch(req,resp, "/views/admin/home.jsp");
                }
            } else {
                /// 跳转到登录页面
                doDispatch(req,resp, "/login.jsp");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    // public void dispatch(HttpServletRequest req, HttpServletResponse res, String
    // path) throws Exception {
    // RequestDispatcher dispatcher = req.getRequestDispatcher(path);
    // dispatcher.forward(req, res);
    // }
}
